{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入鸢尾花数据集\n",
    "data(iris)\n",
    "# 对鸢尾花数据集进行处理，使其和教材上的数据一致\n",
    "data_frame <- iris\n",
    "for (i in 1 : 4) {\n",
    "    data_frame[i] <- iris[i] * 10\n",
    "}\n",
    "names(data_frame) <- c('x1', 'x2', 'x3', 'x4', 'Species')\n",
    "attach(data_frame)\n",
    "library(MASS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Call:\n",
      "qda(Species ~ x2 + x4, data = data_frame)\n",
      "\n",
      "Prior probabilities of groups:\n",
      "    setosa versicolor  virginica \n",
      " 0.3333333  0.3333333  0.3333333 \n",
      "\n",
      "Group means:\n",
      "              x2    x4\n",
      "setosa     34.28  2.46\n",
      "versicolor 27.70 13.26\n",
      "virginica  29.74 20.26\n"
     ]
    }
   ],
   "source": [
    "# 总体协方差矩阵不相同的情况\n",
    "qd1 <- qda(Species ~ x2 + x4, data = data_frame)\n",
    "print(qd1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "            \n",
       "Species      setosa versicolor virginica\n",
       "  setosa         50          0         0\n",
       "  versicolor      0         46         4\n",
       "  virginica       0          3        47"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "0.0466666666666667"
      ],
      "text/latex": [
       "0.0466666666666667"
      ],
      "text/markdown": [
       "0.0466666666666667"
      ],
      "text/plain": [
       "[1] 0.04666667"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "            \n",
       "Species      setosa versicolor virginica\n",
       "  setosa         50          0         0\n",
       "  versicolor      0         45         5\n",
       "  virginica       0          3        47"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "0.0533333333333333"
      ],
      "text/latex": [
       "0.0533333333333333"
      ],
      "text/markdown": [
       "0.0533333333333333"
      ],
      "text/plain": [
       "[1] 0.05333333"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#误判概率\n",
    "pred2 <- predict(qd1)\n",
    "table(Species, pred2 $ class)\n",
    "error3 <- data.frame(pred2, Species)[pred2 $ class != Species,]\n",
    "length(error3 $ class) / length(pred2 $ class)\n",
    "# 交叉确认误判率\n",
    "qd2 <- qda(Species ~ x2 + x4, data = data_frame, CV = TRUE)\n",
    "table(Species, qd2 $ class)\n",
    "error4 <- data.frame(pred2, Species)[qd2 $ class != Species,]\n",
    "length(error4 $ class) / length(pred2 $ class)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Call:\n",
      "lda(Species ~ x2 + x4, data = data_frame)\n",
      "\n",
      "Prior probabilities of groups:\n",
      "    setosa versicolor  virginica \n",
      " 0.3333333  0.3333333  0.3333333 \n",
      "\n",
      "Group means:\n",
      "              x2    x4\n",
      "setosa     34.28  2.46\n",
      "versicolor 27.70 13.26\n",
      "virginica  29.74 20.26\n",
      "\n",
      "Coefficients of linear discriminants:\n",
      "          LD1        LD2\n",
      "x2 -0.1986964 0.26800746\n",
      "x4  0.5477136 0.08169648\n",
      "\n",
      "Proportion of trace:\n",
      "   LD1    LD2 \n",
      "0.9884 0.0116 \n"
     ]
    }
   ],
   "source": [
    "# 总体协方差矩阵相同\n",
    "ld1 <- lda(Species ~ x2 + x4, data = data_frame)\n",
    "print(ld1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "            \n",
       "Species      setosa versicolor virginica\n",
       "  setosa         50          0         0\n",
       "  versicolor      0         49         1\n",
       "  virginica       0          4        46"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "0.0333333333333333"
      ],
      "text/latex": [
       "0.0333333333333333"
      ],
      "text/markdown": [
       "0.0333333333333333"
      ],
      "text/plain": [
       "[1] 0.03333333"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "            \n",
       "Species      setosa versicolor virginica\n",
       "  setosa         50          0         0\n",
       "  versicolor      0         48         2\n",
       "  virginica       0          4        46"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "0.04"
      ],
      "text/latex": [
       "0.04"
      ],
      "text/markdown": [
       "0.04"
      ],
      "text/plain": [
       "[1] 0.04"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#误判概率\n",
    "pred1 <- predict(ld1)\n",
    "table(Species, pred1 $ class)\n",
    "error1 <- data.frame(pred1, Species)[pred1 $ class != Species,]\n",
    "length(error1 $ class) / length(pred1 $ class)\n",
    "# 交叉确认误判率\n",
    "ld12 <- lda(Species ~ x2 + x4, data = data_frame, CV = TRUE)\n",
    "table(Species, ld12 $ class)\n",
    "error2 <- data.frame(pred1, Species)[ld12 $ class != Species,]\n",
    "length(error2 $ class) / length(pred1 $ class)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<dl>\n",
       "\t<dt>$class</dt>\n",
       "\t\t<dd>versicolor\n",
       "<details>\n",
       "\t<summary style=display:list-item;cursor:pointer>\n",
       "\t\t<strong>Levels</strong>:\n",
       "\t</summary>\n",
       "\t<style>\n",
       "\t.list-inline {list-style: none; margin:0; padding: 0}\n",
       "\t.list-inline>li {display: inline-block}\n",
       "\t.list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n",
       "\t</style>\n",
       "\t<ol class=list-inline><li>'setosa'</li><li>'versicolor'</li><li>'virginica'</li></ol>\n",
       "</details></dd>\n",
       "\t<dt>$posterior</dt>\n",
       "\t\t<dd><table class=\"dataframe\">\n",
       "<caption>A matrix: 1 × 3 of type dbl</caption>\n",
       "<thead>\n",
       "\t<tr><th></th><th scope=col>setosa</th><th scope=col>versicolor</th><th scope=col>virginica</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><th scope=row>1</th><td>4.964295e-49</td><td>0.6184831</td><td>0.3815169</td></tr>\n",
       "</tbody>\n",
       "</table>\n",
       "</dd>\n",
       "</dl>\n"
      ],
      "text/latex": [
       "\\begin{description}\n",
       "\\item[\\$class] versicolor\n",
       "\\emph{Levels}: \\begin{enumerate*}\n",
       "\\item 'setosa'\n",
       "\\item 'versicolor'\n",
       "\\item 'virginica'\n",
       "\\end{enumerate*}\n",
       "\n",
       "\\item[\\$posterior] A matrix: 1 × 3 of type dbl\n",
       "\\begin{tabular}{r|lll}\n",
       "  & setosa & versicolor & virginica\\\\\n",
       "\\hline\n",
       "\t1 & 4.964295e-49 & 0.6184831 & 0.3815169\\\\\n",
       "\\end{tabular}\n",
       "\n",
       "\\end{description}\n"
      ],
      "text/markdown": [
       "$class\n",
       ":   versicolor\n",
       "**Levels**: 1. 'setosa'\n",
       "2. 'versicolor'\n",
       "3. 'virginica'\n",
       "\n",
       "\n",
       "\n",
       "$posterior\n",
       ":   \n",
       "A matrix: 1 × 3 of type dbl\n",
       "\n",
       "| <!--/--> | setosa | versicolor | virginica |\n",
       "|---|---|---|---|\n",
       "| 1 | 4.964295e-49 | 0.6184831 | 0.3815169 |\n",
       "\n",
       "\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "$class\n",
       "[1] versicolor\n",
       "Levels: setosa versicolor virginica\n",
       "\n",
       "$posterior\n",
       "        setosa versicolor virginica\n",
       "1 4.964295e-49  0.6184831 0.3815169\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<dl>\n",
       "\t<dt>$class</dt>\n",
       "\t\t<dd>virginica\n",
       "<details>\n",
       "\t<summary style=display:list-item;cursor:pointer>\n",
       "\t\t<strong>Levels</strong>:\n",
       "\t</summary>\n",
       "\t<style>\n",
       "\t.list-inline {list-style: none; margin:0; padding: 0}\n",
       "\t.list-inline>li {display: inline-block}\n",
       "\t.list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n",
       "\t</style>\n",
       "\t<ol class=list-inline><li>'setosa'</li><li>'versicolor'</li><li>'virginica'</li></ol>\n",
       "</details></dd>\n",
       "\t<dt>$posterior</dt>\n",
       "\t\t<dd><table class=\"dataframe\">\n",
       "<caption>A matrix: 1 × 3 of type dbl</caption>\n",
       "<thead>\n",
       "\t<tr><th></th><th scope=col>setosa</th><th scope=col>versicolor</th><th scope=col>virginica</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><th scope=row>1</th><td>3.127738e-15</td><td>0.4884176</td><td>0.5115824</td></tr>\n",
       "</tbody>\n",
       "</table>\n",
       "</dd>\n",
       "\t<dt>$x</dt>\n",
       "\t\t<dd><table class=\"dataframe\">\n",
       "<caption>A matrix: 1 × 2 of type dbl</caption>\n",
       "<thead>\n",
       "\t<tr><th></th><th scope=col>LD1</th><th scope=col>LD2</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><th scope=row>1</th><td>2.41037</td><td>1.677103</td></tr>\n",
       "</tbody>\n",
       "</table>\n",
       "</dd>\n",
       "</dl>\n"
      ],
      "text/latex": [
       "\\begin{description}\n",
       "\\item[\\$class] virginica\n",
       "\\emph{Levels}: \\begin{enumerate*}\n",
       "\\item 'setosa'\n",
       "\\item 'versicolor'\n",
       "\\item 'virginica'\n",
       "\\end{enumerate*}\n",
       "\n",
       "\\item[\\$posterior] A matrix: 1 × 3 of type dbl\n",
       "\\begin{tabular}{r|lll}\n",
       "  & setosa & versicolor & virginica\\\\\n",
       "\\hline\n",
       "\t1 & 3.127738e-15 & 0.4884176 & 0.5115824\\\\\n",
       "\\end{tabular}\n",
       "\n",
       "\\item[\\$x] A matrix: 1 × 2 of type dbl\n",
       "\\begin{tabular}{r|ll}\n",
       "  & LD1 & LD2\\\\\n",
       "\\hline\n",
       "\t1 & 2.41037 & 1.677103\\\\\n",
       "\\end{tabular}\n",
       "\n",
       "\\end{description}\n"
      ],
      "text/markdown": [
       "$class\n",
       ":   virginica\n",
       "**Levels**: 1. 'setosa'\n",
       "2. 'versicolor'\n",
       "3. 'virginica'\n",
       "\n",
       "\n",
       "\n",
       "$posterior\n",
       ":   \n",
       "A matrix: 1 × 3 of type dbl\n",
       "\n",
       "| <!--/--> | setosa | versicolor | virginica |\n",
       "|---|---|---|---|\n",
       "| 1 | 3.127738e-15 | 0.4884176 | 0.5115824 |\n",
       "\n",
       "\n",
       "$x\n",
       ":   \n",
       "A matrix: 1 × 2 of type dbl\n",
       "\n",
       "| <!--/--> | LD1 | LD2 |\n",
       "|---|---|---|\n",
       "| 1 | 2.41037 | 1.677103 |\n",
       "\n",
       "\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "$class\n",
       "[1] virginica\n",
       "Levels: setosa versicolor virginica\n",
       "\n",
       "$posterior\n",
       "        setosa versicolor virginica\n",
       "1 3.127738e-15  0.4884176 0.5115824\n",
       "\n",
       "$x\n",
       "      LD1      LD2\n",
       "1 2.41037 1.677103\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 新样本\n",
    "x0 <- data.frame(x2 = 35, x4 = 18)\n",
    "# predict\n",
    "predict(qd1, newdata = x0)\n",
    "predict(ld1, newdata = x0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Call:\n",
      "qda(Species ~ ., data = data_frame)\n",
      "\n",
      "Prior probabilities of groups:\n",
      "    setosa versicolor  virginica \n",
      " 0.3333333  0.3333333  0.3333333 \n",
      "\n",
      "Group means:\n",
      "              x1    x2    x3    x4\n",
      "setosa     50.06 34.28 14.62  2.46\n",
      "versicolor 59.36 27.70 42.60 13.26\n",
      "virginica  65.88 29.74 55.52 20.26\n"
     ]
    }
   ],
   "source": [
    "qd3 <- qda(Species ~ ., data = data_frame)\n",
    "print(qd3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "            \n",
       "Species      setosa versicolor virginica\n",
       "  setosa         50          0         0\n",
       "  versicolor      0         48         2\n",
       "  virginica       0          1        49"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "0.02"
      ],
      "text/latex": [
       "0.02"
      ],
      "text/markdown": [
       "0.02"
      ],
      "text/plain": [
       "[1] 0.02"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "            \n",
       "Species      setosa versicolor virginica\n",
       "  setosa         50          0         0\n",
       "  versicolor      0         47         3\n",
       "  virginica       0          1        49"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "0.0266666666666667"
      ],
      "text/latex": [
       "0.0266666666666667"
      ],
      "text/markdown": [
       "0.0266666666666667"
      ],
      "text/plain": [
       "[1] 0.02666667"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#误判概率\n",
    "pred3 <- predict(qd3)\n",
    "table(Species, pred3 $ class)\n",
    "error5 <- data.frame(pred3, Species)[pred3 $ class != Species,]\n",
    "length(error5 $ class) / length(pred3 $ class)\n",
    "# 交叉确认误判率\n",
    "qd4 <- qda(Species ~ ., data = data_frame, CV = TRUE)\n",
    "table(Species, qd4 $ class)\n",
    "error6 <- data.frame(pred3, Species)[qd4 $ class != Species,]\n",
    "length(error6 $ class) / length(pred3 $ class)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Call:\n",
      "lda(Species ~ ., data = data_frame)\n",
      "\n",
      "Prior probabilities of groups:\n",
      "    setosa versicolor  virginica \n",
      " 0.3333333  0.3333333  0.3333333 \n",
      "\n",
      "Group means:\n",
      "              x1    x2    x3    x4\n",
      "setosa     50.06 34.28 14.62  2.46\n",
      "versicolor 59.36 27.70 42.60 13.26\n",
      "virginica  65.88 29.74 55.52 20.26\n",
      "\n",
      "Coefficients of linear discriminants:\n",
      "           LD1          LD2\n",
      "x1  0.08293776  0.002410215\n",
      "x2  0.15344731  0.216452123\n",
      "x3 -0.22012117 -0.093192121\n",
      "x4 -0.28104603  0.283918785\n",
      "\n",
      "Proportion of trace:\n",
      "   LD1    LD2 \n",
      "0.9912 0.0088 \n"
     ]
    }
   ],
   "source": [
    "# 使用全部指标\n",
    "ld2 <- lda(Species ~ ., data = data_frame)\n",
    "print(ld2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "            \n",
       "Species      setosa versicolor virginica\n",
       "  setosa         50          0         0\n",
       "  versicolor      0         48         2\n",
       "  virginica       0          1        49"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "0.02"
      ],
      "text/latex": [
       "0.02"
      ],
      "text/markdown": [
       "0.02"
      ],
      "text/plain": [
       "[1] 0.02"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "            \n",
       "Species      setosa versicolor virginica\n",
       "  setosa         50          0         0\n",
       "  versicolor      0         48         2\n",
       "  virginica       0          1        49"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "0.02"
      ],
      "text/latex": [
       "0.02"
      ],
      "text/markdown": [
       "0.02"
      ],
      "text/plain": [
       "[1] 0.02"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#误判概率\n",
    "pred4 <- predict(ld2)\n",
    "table(Species, pred4 $ class)\n",
    "error7 <- data.frame(pred4, Species)[pred4 $ class != Species,]\n",
    "length(error7 $ class) / length(pred4 $ class)\n",
    "# 交叉确认误判率\n",
    "ld22 <- lda(Species ~ ., data = data_frame, CV = TRUE)\n",
    "table(Species, ld22 $ class)\n",
    "error8 <- data.frame(pred4, Species)[ld22 $ class != Species,]\n",
    "length(error8 $ class) / length(pred4 $ class)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Call:\n",
      "qda(Species ~ x1 + x3, data = data_frame)\n",
      "\n",
      "Prior probabilities of groups:\n",
      "    setosa versicolor  virginica \n",
      " 0.3333333  0.3333333  0.3333333 \n",
      "\n",
      "Group means:\n",
      "              x1    x3\n",
      "setosa     50.06 14.62\n",
      "versicolor 59.36 42.60\n",
      "virginica  65.88 55.52\n"
     ]
    }
   ],
   "source": [
    "# X1 X3\n",
    "qd3 <- qda(Species ~ x1 + x3, data = data_frame)\n",
    "print(qd3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "            \n",
       "Species      setosa versicolor virginica\n",
       "  setosa         50          0         0\n",
       "  versicolor      0         47         3\n",
       "  virginica       0          3        47"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "0.04"
      ],
      "text/latex": [
       "0.04"
      ],
      "text/markdown": [
       "0.04"
      ],
      "text/plain": [
       "[1] 0.04"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "            \n",
       "Species      setosa versicolor virginica\n",
       "  setosa         50          0         0\n",
       "  versicolor      0         47         3\n",
       "  virginica       0          3        47"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "0.04"
      ],
      "text/latex": [
       "0.04"
      ],
      "text/markdown": [
       "0.04"
      ],
      "text/plain": [
       "[1] 0.04"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#误判概率\n",
    "pred5 <- predict(qd3)\n",
    "table(Species, pred5 $ class)\n",
    "error9 <- data.frame(pred5, Species)[pred5 $ class != Species,]\n",
    "length(error9 $ class) / length(pred5 $ class)\n",
    "# 交叉确认误判率\n",
    "qd4 <- qda(Species ~ x1 + x3, data = data_frame, CV = TRUE)\n",
    "table(Species, qd4 $ class)\n",
    "error10 <- data.frame(pred5, Species)[qd4 $ class != Species,]\n",
    "length(error10 $ class) / length(pred5 $ class)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Call:\n",
      "lda(Species ~ x1 + x3, data = data_frame)\n",
      "\n",
      "Prior probabilities of groups:\n",
      "    setosa versicolor  virginica \n",
      " 0.3333333  0.3333333  0.3333333 \n",
      "\n",
      "Group means:\n",
      "              x1    x3\n",
      "setosa     50.06 14.62\n",
      "versicolor 59.36 42.60\n",
      "virginica  65.88 55.52\n",
      "\n",
      "Coefficients of linear discriminants:\n",
      "          LD1         LD2\n",
      "x1 -0.1658896  0.24617343\n",
      "x3  0.3427642 -0.09282016\n",
      "\n",
      "Proportion of trace:\n",
      "   LD1    LD2 \n",
      "0.9987 0.0013 \n"
     ]
    }
   ],
   "source": [
    "ld3 <- lda(Species ~ x1 + x3, data = data_frame)\n",
    "print(ld3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "            \n",
       "Species      setosa versicolor virginica\n",
       "  setosa         50          0         0\n",
       "  versicolor      0         48         2\n",
       "  virginica       0          3        47"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "0.0333333333333333"
      ],
      "text/latex": [
       "0.0333333333333333"
      ],
      "text/markdown": [
       "0.0333333333333333"
      ],
      "text/plain": [
       "[1] 0.03333333"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "            \n",
       "Species      setosa versicolor virginica\n",
       "  setosa         50          0         0\n",
       "  versicolor      0         48         2\n",
       "  virginica       0          4        46"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "0.04"
      ],
      "text/latex": [
       "0.04"
      ],
      "text/markdown": [
       "0.04"
      ],
      "text/plain": [
       "[1] 0.04"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#误判概率\n",
    "pred6 <- predict(ld3)\n",
    "table(Species, pred6 $ class)\n",
    "error11 <- data.frame(pred6, Species)[pred6 $ class != Species,]\n",
    "length(error11 $ class) / length(pred6 $ class)\n",
    "# 交叉确认误判率\n",
    "ld32 <- lda(Species ~ x1 + x3, data = data_frame, CV = TRUE)\n",
    "table(Species, ld32 $ class)\n",
    "error12 <- data.frame(pred6, Species)[ld32 $ class != Species,]\n",
    "length(error12 $ class) / length(pred6 $ class)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R",
   "language": "R",
   "name": "ir"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "4.1.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
